﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Beat_em_Up
{
     class Player : Object3D
    {
        bool isAtTheMaxHeight = false;
        bool wasPressed = false;
        
        public Player (Model model, Vector3 pos) : base(model, pos, 1f)
        {
            this.model = model;
            this.position = pos;
        }
        
        public void playerMovement(GameTime gameTime, KeyboardState currentKeyboard, KeyboardState oldKeyboard)
        {
            float acceleration = 0.1f;
            //float speed = 0f;
            //float time = (float)(gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f);

            currentKeyboard = Keyboard.GetState();

            if (currentKeyboard.IsKeyDown(Keys.D) || currentKeyboard.IsKeyDown(Keys.Right))
            {
                position.X += acceleration;//(speed + time) +  ((acceleration * (float)Math.Pow(time, 2)) / 2.0f);
            }
                
            else if (currentKeyboard.IsKeyDown(Keys.A) || currentKeyboard.IsKeyDown(Keys.Left))
            {
                position.X -= acceleration;//(speed + time) + ((acceleration * (float)Math.Pow(time, 2)) / 2.0f);
            }

            if ((currentKeyboard.IsKeyDown(Keys.W) && oldKeyboard.IsKeyUp(Keys.W)) || (currentKeyboard.IsKeyDown(Keys.Up) && oldKeyboard.IsKeyUp(Keys.Up)))
            {
                wasPressed = true;
            }

            #region [Jump]
           
            if (wasPressed == true)
            {
                position.Y += acceleration;
            }

            if (position.Y >= 2)
            {
                wasPressed = false;
                isAtTheMaxHeight = true;
            }
            

            if (isAtTheMaxHeight == true)
            {
                position.Y -= acceleration;
            }

            if (position.Y <= 0)
            {
                isAtTheMaxHeight = false;
            }

            #endregion

            //Console.WriteLine(wasPressed + " vs " + isAtTheMaxHeight);
            
            oldKeyboard = currentKeyboard;
        }

         // Esse update está com problema, não sei como, não sei porque,
         // mas mesmo ele sendo chamado no Scene_01, ele não é executado.
        public void Update(GameTime gameTime, KeyboardState currentKeyboard)
        {
            //playerMovement(currentKeyboard);
            base.UpdateBoundingBox(model, world);
            base.Update(gameTime);
        }
    }
}
